package com.ayuku.commons;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;


/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public class AyukuCalComp extends org.eclipse.swt.widgets.Dialog {
    private Shell dialogShell;
    private DateTime text1;
    private DateTime dt;

    public AyukuCalComp(Shell parent, int style, DateTime dt) {
        super(parent, style);
        this.dt = dt;
    }

    public final DateTime getDt() {
        return dt;
    }

    public final void setDt(DateTime dt) {
        this.dt = dt;
    }

    public void open() {
        try {
            Shell parent = getParent();
            dialogShell = new Shell(parent, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);

            FillLayout dialogShellLayout = new FillLayout(org.eclipse.swt.SWT.HORIZONTAL);
            dialogShell.setLayout(dialogShellLayout);
            text1 = new DateTime(dialogShell, SWT.CALENDAR | SWT.NONE);
            text1.addMouseListener(new MouseAdapter() {
                    public void mouseDoubleClick(MouseEvent evt) {
                        text1MouseDoubleClick(evt);
                    }

                    public void mouseUp(MouseEvent evt) {
                        text1MouseUp(evt);
                    }
                });

            text1.addSelectionListener(new SelectionAdapter() {
                    public void widgetSelected(SelectionEvent evt) {
                        text1WidgetSelected(evt);
                    }
                });

            text1.setDay(getDt().getDay());
            text1.setMonth(getDt().getMonth());
            text1.setYear(getDt().getYear());

            dialogShell.layout();
            dialogShell.pack();
            dialogShell.setLocation(getParent().toDisplay(100, 100));
            dialogShell.open();

            Display display = dialogShell.getDisplay();

            while (!dialogShell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void text1WidgetSelected(SelectionEvent evt) {
    }

    public void doSomething() {
    }

    private void text1MouseUp(MouseEvent evt) {
        System.out.println("mouse up");
        dt.setDay(text1.getDay());
        dt.setMonth(text1.getMonth());
        dt.setYear(text1.getYear());

        System.out.println(text1.getDay());
        System.out.println(text1.getMonth());
        System.out.println(text1.getYear());
        doSomething();
    }

    private void text1MouseDoubleClick(MouseEvent evt) {
    }
}
